package itheima.itheima_262;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/*
    需求：
        创建一个HashMap集合，键是学号（String）,值是学生对象（Student），存储三个键值对元素，并遍历

        思路：
            1：定义学生类
            2：创建HashMap集合对象
            3：创建学生对象
            4：把学生添加到集合
            5：遍历集合
                方式1：键找值
                方式2：键值对对象找键和值
 */
public class HashMapDemo {
    public static void main(String[] args) {
        HashMap<String,Student> hm=new HashMap<String, Student>();

        Student s1=new Student("林青霞",30);
        Student s2=new Student("张曼玉",35);
        Student s3=new Student("王祖贤",33);

        hm.put("itheima001",s1);
        hm.put("itheima002",s2);
        hm.put("itheima003",s3);
        //方式一遍历
        Set<String> keySet = hm.keySet();
        for (String key:keySet){
            Student value = hm.get(key);
            System.out.println(key+","+value.getName()+","+value.getAge());
        }
        System.out.println("--------");

        //方式二:键值对对象找键和值
        Set<Map.Entry<String, Student>> entrySet = hm.entrySet();
        for (Map.Entry<String,Student> me: entrySet){
            String key = me.getKey();
            Student value = me.getValue();
            System.out.println(key+","+value.getName()+","+value.getAge());
        }
    }


}
